package com.probiz.estoresa.catalog.web.action;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;

import com.probiz.estore.catalog.service.ContentCategoryManager;
import com.probiz.estore.common.model.catalog.ContentCategory;
import com.probiz.estore.core.controller.GenericStoreAdminController;
import com.probiz.estore.webapp.util.RequestUtil;

public class ContentCategorySelectorController extends	GenericStoreAdminController<ContentCategory> {
	private ContentCategoryManager contentCategoryManager = null;


	public void setContentCategoryManager(
			ContentCategoryManager contentCategoryManager) {
		this.contentCategoryManager = contentCategoryManager;
	}


	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.controller.GenericController#getEntityName(java.lang.Object)
	 */
	@Override
	protected String getEntityName(ContentCategory entity) {
		return entity.getContentCategoryName();
	}


	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.controller.BaseController#initController()
	 */
	@Override
	protected void initController() throws Exception {
		mgr = contentCategoryManager;
	}

	
	@Override
	protected void preSave(HttpServletRequest request, ContentCategory entity,
			BindException errors) {
		
	}

	
	public ModelAndView defaultAction(HttpServletRequest request,
			HttpServletResponse response) {
		ModelAndView mv = new ModelAndView("catalog/contentCategorySelector");
		ContentCategory treeRoot = contentCategoryManager.getById(1);     
        Set<ContentCategory> categorySet=treeRoot.getChildren();
        List<ContentCategory> categoryList=new ArrayList<ContentCategory>();
        categoryList.addAll(categorySet);    
        /**-----------start:去掉了链接目录类型的----------------**/
		Iterator<ContentCategory> iter = categoryList.iterator();
		while(iter.hasNext()){
			ContentCategory contentCategory = iter.next();
			if(contentCategory.getCategoryType().equals(ContentCategory.TYPE_LINK)){
				iter.remove();
			}
		}
		/**-----------end: 去掉了链接目录类型的----------------**/
		mv.addObject("categoryList", categoryList);
	    mv.addObject("treeRoot", treeRoot);
		return mv;
	}
	
	/**
	 * 获取树节点，ajax方式
	 * @param request
	 * @param response
	 * @return
	 */
	public ModelAndView getTreeItems(HttpServletRequest request,HttpServletResponse response) {
		ModelAndView mv = new ModelAndView("catalog/include/contentCategorySelectorTreeAjax");
		String categoryId = RequestUtil.getParameterNullSafe(request, "categoryId");
		ContentCategory parent = contentCategoryManager.getById(Integer.valueOf(categoryId));
		Set<ContentCategory> categorySet = parent.getChildren();
		List<ContentCategory> categoryList = new ArrayList<ContentCategory>();
		categoryList.addAll(categorySet);
		 /**-----------start:去掉了链接目录类型的----------------**/
		Iterator<ContentCategory> iter = categoryList.iterator();
		while(iter.hasNext()){
			ContentCategory contentCategory = iter.next();
			if(contentCategory.getCategoryType().equals(ContentCategory.TYPE_LINK)){
				iter.remove();
			}
		}
		/**-----------end: 去掉了链接目录类型的----------------**/
		mv.addObject("categoryList",categoryList);
		return mv;	
	} 	
	
	
	
	
}
